package com.atguigu.lease.web.admin.task;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * projectName: com.atguigu.lease.web.admin.task
 *
 * @author: wangs
 * description:
 */

@Component
public class UpdateLeaseAgreementStatusTask {

    @Autowired
    private LeaseAgreementService leaseAgreementService;

    // 每天零点零分零秒检查租约日期是否已到期
//    @Scheduled(cron = "0 0 0 * * ?")
    @Scheduled(cron = "0 42 12 * * ?")
    public void updateLeaseAgreementStatus(){
        // 创建 LambdaUpdateWrapper 对象
        LambdaUpdateWrapper<LeaseAgreement> leaseAgreementLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        // 封装查询条件
        // 1. 租约结束日期，小于等于当前日期
        leaseAgreementLambdaUpdateWrapper.le(LeaseAgreement::getLeaseEndDate,new Date());
        // 2. 租约状态 2,5,7
//        leaseAgreementLambdaUpdateWrapper.in(LeaseAgreement::getStatus,2,5,7);
        leaseAgreementLambdaUpdateWrapper.in(LeaseAgreement::getStatus,LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING,LeaseStatus.RENEWING);
        // 设置更新的字段
        leaseAgreementLambdaUpdateWrapper.set(LeaseAgreement::getStatus, LeaseStatus.EXPIRED);
        // 调用带条件的更新
        leaseAgreementService.update(leaseAgreementLambdaUpdateWrapper);
    }
}
